/**
 *  This file is part of MunchLevel.
 *
 *  MunchLevel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MunchLevel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MunchLevel.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.munchlevel.view;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

import com.munchlevel.R;

public class NumberDrawable extends Drawable {

	private static Map<Character, Integer> counterImages = new HashMap<Character, Integer>(12);

	static {
		counterImages.put('0', R.drawable.txt_0);
		counterImages.put('1', R.drawable.txt_1);
		counterImages.put('2', R.drawable.txt_2);
		counterImages.put('3', R.drawable.txt_3);
		counterImages.put('4', R.drawable.txt_4);
		counterImages.put('5', R.drawable.txt_5);
		counterImages.put('6', R.drawable.txt_6);
		counterImages.put('7', R.drawable.txt_7);
		counterImages.put('8', R.drawable.txt_8);
		counterImages.put('9', R.drawable.txt_9);
		counterImages.put('-', R.drawable.txt_minus);
		counterImages.put('+', R.drawable.txt_plus);
	}

	private Bitmap[] drawable;
	private Rect[] mDstRect; // Gravity.apply() sets this
	private char[] chars;

	private int paddingVertical = 66;

	private boolean applyBounds = true;

	private static WeakReference<Map<Character, Bitmap>> bitmaps;

	private int number = -1;

	private Resources resources;

	private Paint paint;

	private boolean showPlus = false;

	public NumberDrawable(Resources res, int number, boolean showPlus) {
		resources = res;
		paint = new Paint();
		setShowPlus(showPlus);
		setNumber(number);
	}

	private Bitmap getBitmaps(char number) {

		Bitmap bitmap = null;
		Map<Character, Bitmap> bitmapMap = null;
		if (bitmaps == null || bitmaps.get() == null) {
			bitmapMap = new HashMap<Character, Bitmap>(12);
			bitmaps = new WeakReference<Map<Character, Bitmap>>(bitmapMap);
		} else {
			bitmapMap = bitmaps.get();
		}

		bitmap = bitmapMap.get(number);
		if (bitmap == null) {

			bitmap = BitmapFactory.decodeResource(resources, counterImages.get(number));
			bitmapMap.put(number, bitmap);
		}

		return bitmap;
	}

	public boolean isShowPlus() {
		return showPlus;
	}

	public void setShowPlus(boolean showPlus) {
		this.showPlus = showPlus;
	}

	@Override
	protected void onBoundsChange(Rect bounds) {
		super.onBoundsChange(bounds);
		applyBounds = true;
	}

	private void applyBounds(Rect bounds) {

		float scale = (float) bounds.height() / (float) getMinimumHeight();
		int scaledPaddingVertical = (int) (paddingVertical * scale);
		int scaledBitmapWidth = (int) (getMinimumWidth() * scale);

		for (int i = 0; i < drawable.length; i++) {

			Bitmap bitmap = drawable[i];
			Rect rect = mDstRect[i];

			float percentage1 = (float) bitmap.getWidth() / (float) getIntrinsicWidth();
			int scaledWidth1 = (int) (scaledBitmapWidth * percentage1);

			if (i == 0)
				rect.left = bounds.centerX() - scaledBitmapWidth / 2;
			else
				rect.left = mDstRect[i - 1].right;
			rect.right = rect.left + scaledWidth1;
			rect.top = bounds.top + scaledPaddingVertical;
			rect.bottom = bounds.bottom - scaledPaddingVertical;

		}

	}

	@Override
	public void draw(Canvas canvas) {

		if (applyBounds)
			applyBounds(getBounds());

		for (int i = 0; i < drawable.length; i++) {
			canvas.drawBitmap(drawable[i], null, mDstRect[i], paint);
		}
	}

	@Override
	public int getOpacity() {
		return PixelFormat.TRANSLUCENT;
	}

	@Override
	public void setAlpha(int a) {
		paint.setAlpha(a);
	}

	@Override
	public void setColorFilter(ColorFilter filter) {
		paint.setColorFilter(filter);
		paint.setFilterBitmap(true);
		paint.setAntiAlias(true);
		paint.setDither(false);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
	}

	@Override
	public int getIntrinsicWidth() {
		int width = 0;
		if (drawable != null) {
			for (int i = 0; i < drawable.length; i++) {
				if (drawable[i] != null)
					width += drawable[i].getWidth();
			}
		} else {
			width = 100;
		}
		return width;

	}

	@Override
	public int getIntrinsicHeight() {
		int height = 0;

		if (drawable != null) {
			for (int i = 0; i < drawable.length; i++) {
				if (drawable[i] != null)
					height = Math.max(height, drawable[i].getHeight());
			}
		} else {
			height = 100;
		}

		return height + paddingVertical * 2;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		if (number > 999)
			number = 999;

		if (number < -99)
			number = -99;

		this.number = number;

		String numberString = Integer.toString(number);
		if (showPlus && number > 0) {
			numberString = "+" + numberString;
		}
		chars = numberString.toCharArray();

		mDstRect = new Rect[chars.length];
		drawable = new Bitmap[chars.length];

		for (int i = 0; i < chars.length; i++) {
			drawable[i] = getBitmaps(chars[i]);
			mDstRect[i] = new Rect();
		}

		applyBounds = true;

	}
}
